﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web.Mvc;

namespace AllWebIdea.Mvc
{
    /// <summary>
    /// Extended the routes
    /// </summary>
    public static class LowercaseRouteExtensions
    {
        /// <summary>
        /// Map a lowercase route
        /// </summary>
        /// <param name="routes"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        public static void MapLowercaseRoute(this RouteCollection routes, string name, string url)
        {
            MapLowercaseRoute(routes, name, url, null /* defaults */, null /* constraints */);
        }

        /// <summary>
        /// Map a lowercase route
        /// </summary>
        /// <param name="routes"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="defaults"></param>
        public static void MapLowercaseRoute(this RouteCollection routes, string name, string url, object defaults)
        {
            MapLowercaseRoute(routes, name, url, defaults, null /* constraints */);
        }

        /// <summary>
        /// Map a lowercase route
        /// </summary>
        /// <param name="routes"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="defaults"></param>
        /// <param name="constraints"></param>
        public static void MapLowercaseRoute(this RouteCollection routes, string name, string url, object defaults, object constraints)
        {
            if (routes == null)
            {
                throw new ArgumentNullException("routes");
            }
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }

            LowercaseRoute route = new LowercaseRoute(url, new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(defaults),
                Constraints = new RouteValueDictionary(constraints)
            };

            if (String.IsNullOrEmpty(name))
            {
                // Add unnamed route if no name given
                routes.Add(route);
            }
            else
            {
                routes.Add(name, route);
            }
        }

    }
}
